/**
 * 项目名称:
 *
 * @Author: 夏国倩
 * @Date: 2023/10/12 16:33
 * @Copyright
 */
package multithreading;

public class ThreadTest18 {
    public static void main(String[] args) throws InterruptedException {
        MyClass4 mc1 = new MyClass4();
        MyClass4 mc2 = new MyClass4();

        Thread t1 = new Thread(new Processor18(mc1));
        Thread t2 = new Thread(new Processor18(mc2));

        t1.setName("t1");
        t2.setName("t2");

        //启动线程
        t1.start();

        //延迟(保证t1先启动，并执行run)
        Thread.sleep(1000);
        t2.start();
    }
}

class Processor18 implements Runnable {
    MyClass4 mc;

    public Processor18(MyClass4 mc) {
        this.mc = mc;
    }

    @Override
    public void run() {
        if ("t1".equals(Thread.currentThread().getName())) {
            MyClass3.m1();
        }
        if ("t2".equals(Thread.currentThread().getName())) {
            MyClass3.m2();
        }
    }
}

class MyClass4 {
    public synchronized void m1() {
        //休眠
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("m1.......");
    }

    //
    public synchronized void m2() {
        System.out.println("m2......");
    }
}